package sz.szsmk.citizencard.mvp.dzsmkpresenter;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.functions.Action1;
import sz.szsmk.citizencard.model.network.dzbean.req.CommonRequest;
import sz.szsmk.citizencard.model.network.dzbean.res.BankCard;
import sz.szsmk.citizencard.model.network.dzbean.res.GetMyBankResponse;
import sz.szsmk.citizencard.mvp.MyBankCardMvpView;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.HttpApi;
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource;
import sz.szsmk.citizencard.util.ObjectSignatureUtil;

/**
 * Created by chensf on 17/10/1.
 */

public class MyBankCardPresenter extends AppBasePresenter<MyBankCardMvpView> {

    @Inject
    NetworkDateSource mNetworkDateSource;
    private ArrayList<BankCard> list = new ArrayList<>();

    @Inject
    public MyBankCardPresenter() {
        getComponent().inject(this);
    }

    public void gainMyBankCard() {
        checkViewAttached();
        HttpApi httpApi = mNetworkDateSource.getHttpApi();

        CommonRequest commonRequest = ObjectSignatureUtil.getCommonRequest();

        mNetworkDateSource.getMyBanks(commonRequest, new Action1<GetMyBankResponse>() {
            @Override
            public void call(GetMyBankResponse getMyBankResponse) {

                if (getMyBankResponse.getCode().equals("0000")) {
                    List<GetMyBankResponse.DataBean> data = getMyBankResponse.getData();
                    if (data == null) {
                        return;
                    }
                    list.clear();
                    for (int i = 0; i < data.size(); i++) {
                        GetMyBankResponse.DataBean dataBean = data.get(i);
                        String json = new Gson().toJson(dataBean);
                        BankCard bankCard = new Gson().fromJson(json, BankCard.class);

                        list.add(bankCard);
                    }
                    getMvpView().onGainMyBankCard(list);
                } else {
                    getMvpView().onGainMyBankCardFail(getMyBankResponse);
                }

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }
}
